//
//  MineVC.m
//  qulvyou
//
//  Created by mmyy on 15/10/23.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "MineVC.h"

#import "LogVC.h"

#import "RDVTabBarController.h"

#import "Myshoucang.h"
@interface MineVC ()<UITableViewDataSource, UITableViewDelegate, LogVcDelegate>

@end

@implementation MineVC
{
    UITableView *_tableView;
    NSArray *_dataSource;
    BOOL bl;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"个人中心";
    self.view.backgroundColor = [UIColor purpleColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
}
-(void)buttonClick:(UIButton *)bt
{
    [self log];
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!bl) {
        NSString *access_token = [[NSUserDefaults standardUserDefaults] objectForKey:ACCESS_TOKEN];
        if (access_token == nil || [Help isExpireIn]) {
            [self log];
        }
        if ([[NSUserDefaults standardUserDefaults] valueForKey:SCREEN_NAME]) {
            if (!_dataSource.count) {
                [self loadData];
            }
            
            
        }
    }
   
}
-(void)logFaile
{
    bl = YES;
    [self loadData];
}
-(void)loadData
{
    NSArray *array1 = @[@"我的收藏", @"更改头像", @"清空缓存"];
    NSArray *array2 = @[@"精品工具应用", @"软件地图设置", @"连接社交网络", @"更改头像"];
    NSArray *array3 = @[@"关于我们", @"检查更新", @"意见反馈"];
    _dataSource = @[array1, array2, array3];
    [self createTableView];
    if (bl) {
        UIButton *right = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
        [right setTitle:@"登录" forState:UIControlStateNormal];
        [right setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
        [right addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        right.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    }
}
-(void)log
{
    LogVC *vc = [[LogVC alloc] init];
    vc.delegate = self;
    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nvc animated:YES completion:nil];
}
-(void)createHeadView
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, 200)];
    UIImageView *headImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)];
    headImage.image = [UIImage imageNamed:@"user_bg.jpg"];
    [headView addSubview:headImage];
    UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)];
    blackView.backgroundColor = [UIColor blackColor];
    blackView.alpha = 0.2;
    [headImage addSubview:blackView];
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, self.view.frame.size.width, 30)];
    if (bl) {
        name.text = @"未登录";
    }else {
        name.text = [[NSUserDefaults standardUserDefaults] objectForKey:SCREEN_NAME];
    }
    
    name.textColor = [UIColor whiteColor];
    name.font = [UIFont boldSystemFontOfSize:22];
    name.textAlignment = NSTextAlignmentCenter;
    name.backgroundColor = [UIColor clearColor];
    [headView addSubview:name];
    
    UIImageView *aval = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 60) / 2.0, 50, 60, 60)];
    [aval setImageWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:PROFILE_IMAGE_URL]] placeholderImage:[UIImage imageNamed:@"define"]];
    NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:PROFILE_IMAGE_URL]);
    aval.layer.cornerRadius = 30;
    aval.layer.masksToBounds = YES;
    [headView addSubview:aval];
    
    UILabel *guangzhu = [[UILabel alloc] initWithFrame:CGRectMake(0, 120, self.view.frame.size.width, 20)];
    if (bl) {
        guangzhu.text = [NSString stringWithFormat:@"关注:0 / 粉丝:0"];
    }else {
        guangzhu.text = [NSString stringWithFormat:@"关注%@ / 粉丝%@", [[NSUserDefaults standardUserDefaults] objectForKey:FOLLOWERS_COUNT], [[NSUserDefaults standardUserDefaults] objectForKey:FRIENDS_COUNT]];
    }
    
    
    guangzhu.textColor = [UIColor whiteColor];
    guangzhu.font = [UIFont systemFontOfSize:15];
    guangzhu.textAlignment = NSTextAlignmentCenter;
    guangzhu.backgroundColor = [UIColor clearColor];
    [headView addSubview:guangzhu];
    _tableView.tableHeaderView = headView;
    
}

-(void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height - 65) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    [self createHeadView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_dataSource objectAtIndex:section] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"basis_Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = [[_dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 0 && indexPath.section == 0) {
        Myshoucang *shoucan = [[Myshoucang alloc] init];
        [self.navigationController pushViewController:shoucan animated:YES];
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"暂未开放该服务，敬请等待" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }

}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.rdv_tabBarController setTabBarHidden:NO animated:NO];
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    bl = NO;
    _dataSource = nil;
    [_tableView removeFromSuperview];
    [self.rdv_tabBarController setTabBarHidden:YES animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
